// Learn cc.Class:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html

var Global = require('global');

cc.Class({
    extends: cc.Component,

    properties: {
        // foo: {
        //     // ATTRIBUTES:
        //     default: null,        // The default value will be used only when the component attaching
        //                           // to a node for the first time
        //     type: cc.SpriteFrame, // optional, default is typeof default
        //     serializable: true,   // optional, default is true
        // },
        // bar: {
        //     get () {
        //         return this._bar;
        //     },
        //     set (value) {
        //         this._bar = value;
        //     }
        // },
        historyMax:{
            type:cc.Label,
            default:null,
        },
        score:{
            type:cc.Label,
            default:null,
        },
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start () {
        var score = Global.score;
        var historyMax = Global.historyMax;
        this.score.string = score;
        if(score>historyMax){
            Global.historyMax = score;
            this.historyMax.string = score;
        }else{
            this.historyMax.string = historyMax;
        }
        //最高分存储
        console.log("CC_WECHATGAME:"+CC_WECHATGAME);
        if(CC_WECHATGAME){
            var openDataContext = wx.getOpenDataContext();
            openDataContext.postMessage({
                action:'saveRank',
                score:Global.historyMax.toString(),
            });
        }
    },
    btn_restart_click(){
        cc.log("重新开始");
        cc.director.loadScene('start');
    },
    btn_quitGame_click(){
        cc.log('退出游戏');
        if(CC_WECHATGAME){
            wx.exitMiniProgram();
        }else{
            cc.game.end();
        }
    },

    // update (dt) {},
});
